package com.xtayfjpk.security.abdera;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import org.apache.abdera.Abdera;
import org.apache.abdera.factory.Factory;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Element;
import org.apache.abdera.model.Entry;
import org.apache.abdera.security.AbderaSecurity;
import org.apache.abdera.security.Encryption;
import org.apache.abdera.security.EncryptionOptions;

public class EncryptionDemo {

	public static void main(String[] args) throws Exception {
		Abdera abdera = new Abdera();
		Factory factory = abdera.getFactory();
		
		Entry entry = factory.newEntry();
		entry.setId("urn:EncryptionExample/3263827");
	    entry.addAuthor("Nick Chase");
	    entry.setTitle("Apache Abdera eases Atom");
	    entry.setContent("Tired of making raw HTTP calls?  Now "+
	    		         "you can use a new package from Apache ...");
	    entry.addLink("http://incubator.apache.org/abdera/");
	    entry.setUpdated(new java.util.Date());
	    //entry.writeTo(System.out);
	    
	    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
	    keyGenerator.init(128);
	    SecretKey secretKey = keyGenerator.generateKey();
	    
	    AbderaSecurity abderaSecurity = new AbderaSecurity(abdera);
	    Encryption encryption = abderaSecurity.getEncryption();
	    EncryptionOptions options = encryption.getDefaultEncryptionOptions();
	    options.setDataEncryptionKey(secretKey);
	    Document<Element> document = encryption.encrypt(entry.getDocument(), options);
	    document.writeTo(System.out);
	}

}
